package com.yesun.dcm.rpc.protocol;

import com.yesun.dcm.commons.extension.ExtensionLoader;
import com.yesun.dcm.commons.rpc.URL;
import com.yesun.dcm.remote.Server;
import com.yesun.dcm.remote.Transporter;
import com.yesun.dcm.rpc.Exporter;
import com.yesun.dcm.rpc.Invoker;

import java.util.Map;

public class DcmProtocol<T> extends AbstractProtocol<T> {

    @Override
    public Invoker<T> refer(Class<T> type, URL url) {
        DcmInvoker<T> invoker = new DcmInvoker<T>(type, url);
        return invoker;
    }

    @Override
    public void destroy() {

    }

    @Override
    public Exporter<T> export(final Invoker<T> invoker, final Map<String, Object> map) {
        DcmExporter exporter = new DcmExporter(invoker);
        //发布服务，本质就是打开端口，接受请求
        openServer(invoker, map);
        return exporter;
    }

    /**
     * 打开服务
     * @param invoker
     * @param map
     */
    private void openServer(Invoker invoker, Map<String, Object> map) {
        Transporter transporter = ExtensionLoader.getExtensionLoader(Transporter.class).getAdaptiveExtension();
        Server server = transporter.bind(invoker.getURL(), map);
    }

}
